﻿using OAZCSharpSdk.OAZ.Plugin.Extensions;
using OAZCSharpSdk.OAZ.Plugin.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OAZCSharpSdk.OAZ.Plugin.Events
{
    public class PluginEventArgs : EventArgs
    {
        public PluginEventArgs(long RobotQQ)
        {
            this.RobotQQ = RobotQQ;
        }

        /// <summary>
        /// 部分事件无机器人QQ时，需要手动指定才可调用API
        /// </summary>
        /// <param name="RobotQQ"></param>
        public void SetEventRobot(long RobotQQ)
        {
            this.RobotQQ = RobotQQ;
        }

        /// <summary>
        /// 触发该事件的机器人
        /// </summary>
        public long RobotQQ { get; private set; }

        /// <summary>
        /// 在框架记录页输出一行信息
        /// </summary>
        /// <param name="content">输出的内容</param>
        public void OutPut(string content) => OAZ.Api_Output(content);

        /// <summary>
        /// 发送好友消息
        /// </summary>
        /// <param name="FriendQQ"></param>
        /// <param name="MsgContent">消息内容</param>
        /// <param name="MsgRandom">留空则自动生成，如需撤回消息请自行填入</param>
        /// <param name="MsgReq">留空则自动生成，如需撤回消息请自行填入</param>
        /// <returns>成功返回10位时间戳，用于撤回消息用</returns>
        public int SendFriendMessage(long FriendQQ, string MsgContent, int MsgReq = 0, long MsgRandom = 0) => OAZ.Api_SendFriendMsg(RobotQQ, FriendQQ, MsgContent, MsgReq, MsgRandom);

        /// <summary>
        /// 发送好友XML消息
        /// </summary>
        /// <param name="FriendQQ"></param>
        /// <param name="MsgContent">消息内容</param>
        /// <param name="MsgRandom">留空则自动生成，如需撤回消息请自行填入</param>
        /// <param name="MsgReq">留空则自动生成，如需撤回消息请自行填入</param>
        /// <returns>成功返回10位时间戳，用于撤回消息用</returns>
        public int SendFriendXMLMessage(long FriendQQ, string MsgContent, int MsgReq = 0, long MsgRandom = 0) => OAZ.Api_SendFriendXMLMsg(RobotQQ, FriendQQ, MsgContent, MsgReq, MsgRandom);


        /// <summary>
        /// 发送群消息
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="MsgContent">消息内容</param>
        /// <param name="Anonymous">匿名 默认:否</param>
        /// <returns>成功返回10位时间戳</returns>
        public int SendGroupMessage(long GroupQQ, string MsgContent, bool Anonymous = false) => OAZ.Api_SendGroupMsg(RobotQQ, GroupQQ, MsgContent, Anonymous);

        /// <summary>
        /// 发送群XML消息
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="MsgContent">消息内容</param>
        /// <param name="Anonymous">匿名 默认:否</param>
        public int SendGroupXMLMessage(long GroupQQ, string MsgContent, bool Anonymous = false) => OAZ.Api_SendGroupXMLMsg(RobotQQ, GroupQQ, MsgContent, Anonymous);

        /// <summary>
        /// 群礼物_抽取
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <returns>8000-没有抽到|7779-等级不够</returns>
        public int DrawLuckyGift(long GroupQQ) => OAZ.Api_draw_lucky_gift(RobotQQ, GroupQQ);

        /// <summary>
        /// 手机会员网页签到
        /// </summary>
        /// <returns></returns>
        public bool VipQianDaoWeb() => OAZ.Api_vip_qiandao_web(RobotQQ);

        /// <summary>
        /// 公众号会员签到
        /// </summary>
        /// <returns></returns>
        public bool VipPubSubbuttonQianDao() => OAZ.Api_vip_pub_subbutton_qiandao(RobotQQ);

        /// <summary>
        /// 设置或取消群管理
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="QQ">对方QQ</param>
        /// <param name="Set">设置为管理，true为上管理，false为下管理</param>
        /// <returns></returns>
        public bool SetManager(long GroupQQ, long QQ, bool Set) => OAZ.Api_SetManager(RobotQQ, GroupQQ, QQ, Set ? 1 : 0);

        /// <summary>
        /// 踢出群成员
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="QQ">对方QQ</param>
        /// <param name="Always">拒绝加群申请</param>
        /// <returns></returns>
        public bool RemoveMember(long GroupQQ, long QQ, bool Always) => OAZ.Api_RemoveMember(RobotQQ, GroupQQ, QQ, Always);

        /// <summary>
        /// 取Skey
        /// </summary>
        /// <returns>Skey</returns>
        public string GetSkey() => OAZ.Api_GetSkey(RobotQQ);

        /// <summary>
        /// 取网页Cookie
        /// </summary>
        /// <param name="pt_aid">pt_aid</param>
        /// <param name="aid">aid</param>
        /// <param name="ul">ul</param>
        /// <returns>网页Cookie</returns>
        public async Task<string> GetWebCookies(string pt_aid, string aid, string ul) => await OAZ.GetWebCookies(RobotQQ, pt_aid, aid, ul);


        /// <summary>
        /// 取Pskey
        /// </summary>
        /// <param name="domain">域</param>
        /// <returns></returns>
        public string GetPskey(OAZ.PsKeyDomainEnum domain)
        {
            string GetDomain(OAZ.PsKeyDomainEnum d)
            {
                switch (domain)
                {
                    case OAZ.PsKeyDomainEnum.SuperKey:return string.Empty;
                    case OAZ.PsKeyDomainEnum.TenPay:return "tenpay.qq.com";
                    case OAZ.PsKeyDomainEnum.QianBao:return "qianbao.qq.com";
                    case OAZ.PsKeyDomainEnum.Qun:return "qun.qq.com";
                    case OAZ.PsKeyDomainEnum.OpenMobile: return "openmobile.qq.com";
                    case OAZ.PsKeyDomainEnum.VIP:return "vip.qq.com";
                    case OAZ.PsKeyDomainEnum.Ivac:return "ivac.qq.com";
                    case OAZ.PsKeyDomainEnum.ImgCache:return "imgcache.qq.com";
                    case OAZ.PsKeyDomainEnum.Connect:return "connect.qq.com";
                    case OAZ.PsKeyDomainEnum.Game:return "game.qq.com";
                    case OAZ.PsKeyDomainEnum.GameCenter: return "gamecenter.qq.com";
                    case OAZ.PsKeyDomainEnum.Hall:return "hall.qq.com";
                    case OAZ.PsKeyDomainEnum.HaoMa: return "haoma.qq.com";
                    default:
                        throw new Exception("wft???");
                }
            }
            return OAZ.Api_GetPskey(RobotQQ, GetDomain(domain));
        }

        /// <summary>
        /// 取所有QQ
        /// </summary>
        /// <returns>所有QQ</returns>
        public List<long> GetAllQQ() => OAZ.Api_GetAllQQ().Split(Environment.NewLine.ToCharArray()).Select(x => long.Parse(x)).ToList();

        /// <summary>
        /// 取Clientkey
        /// </summary>
        /// <returns>Clientkey</returns>
        public string GetClientkey() => OAZ.Api_GetClientkey(RobotQQ);

        /// <summary>
        /// 取在线QQ
        /// </summary>
        /// <returns>在线QQ列表</returns>
        public List<long> GetOnlineQQ() {
            string list = OAZ.Api_GetOnlineQQ();
            if (string.IsNullOrWhiteSpace(list))
            {
                return new List<long>();
            }
            return list.Split(Environment.NewLine.ToCharArray()).Select(x => long.Parse(x)).ToList();
        }

        /// <summary>
        /// 取群列表
        /// </summary>
        /// <returns>群列表</returns>
        public List<GroupInfo> GetGroups() => OAZ.Api_GetGroups(RobotQQ).JsonDeserialize<List<GroupInfo>>();

        /// <summary>
        /// 上传本地图片
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="path">图片路径</param>
        /// <returns>图片代码</returns>
        public string UploadGroupPic(long GroupQQ, string path) => OAZ.Picture(RobotQQ, GroupQQ, path);

        /// <summary>
        /// 上传本地图片(base64方式)
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <param name="path">图片路径</param>
        /// <returns>图片代码</returns>
        public string UploadGroupPicEx64(long GroupQQ, string path) => OAZ.Picture2(RobotQQ, GroupQQ, path);

        /// <summary>
        /// 获取登录二维码
        /// </summary>
        /// <param name="RobotQQ">机器人QQ</param>
        /// <returns>二维码信息</returns>
        public QrCodeInfo GetQrcode(long RobotQQ) => OAZ.Api_GetQrcode(RobotQQ.ToString()).JsonDeserialize<QrCodeInfo>();

        /// <summary>
        /// 获取指定QQ在指定群里是否被禁言
        /// </summary>
        /// <param name="GroupQQ">群号</param>
        /// <returns>0=没有被禁言，否则为剩余禁言秒数</returns>
        public int GetOthIsShutUp(long GroupQQ, long QQ) => OAZ.Api_getOthIsShutup(RobotQQ, GroupQQ, QQ);
    }
}
